# -*- coding: utf-8 -*-
# created on 2017/03/14

from sympy import sympify, Eq
from mathsolver.functions.base import BaseFunction, BaseNumber, BaseFunc, BaseFuncEq, new_latex
from mathsolver.functions.hanshu.duicheng_jisuan import DuiChengZhongXing
from mathsolver.functions.hanshu.duichengzhou import HanShuDuiChengZhou
from mathsolver.functions.hanshu.hanshu_inference import normalize_eq
from mathsolver.functions.hanshu.zhouqi import is_m_minus_faminusx, is_faminusx
from mathsolver.functions.sympy_utils import default_symbol


class JiaoDianDuiCheng001(BaseFunction):
    """同一对称性求两函数的交点坐标关系 类型一"""

    @staticmethod
    def get_canshu(arg):
        if isinstance(arg, BaseFuncEq):
            # arg 是关系式
            eq_o = Eq(*sympify(arg.value))
            # 转化成 f(x) = ... 的格式
            eq = normalize_eq(eq_o)
            # f(x) = 2b-f(2a-x) 格式 提取出 a
            expr = eq.rhs
            a = is_m_minus_faminusx(expr) / 2
            m, _ = expr.as_independent(default_symbol(expr))
            b = m / 2
            return a, b
        elif isinstance(arg, BaseFunc):
            # arg 是函数表达式，求函数对称中心
            a, b = DuiChengZhongXing().solver(arg).output[0].sympify()
            return a, b
        else:
            raise ValueError('arg must be of type BaseFunceq or BaseFunc')

    def solver(self, *args):
        sum_obj = args[-1]
        center = self.get_canshu(args[0])
        a, b = center
        m = sum_obj.args[1][2]

        expr = sum_obj.args[0]
        if expr.is_Add:
            res = m * (a + b)
        elif 'x' in str(expr):
            res = m * a
        else:
            res = m * b

        self.steps.append(["", "由题意可推导出两个函数都关于点 (%s, %s) 中心对称" % (a, b)])
        self.steps.append(["", "所以 %s = %s" % (new_latex(sum_obj), res)])
        self.output.append(BaseNumber(res))
        return self


class JiaoDianDuiCheng002(BaseFunction):
    """同一对称性求两函数的交点坐标关系 类型二"""

    @staticmethod
    def get_canshu(arg):
        if isinstance(arg, BaseFuncEq):
            # arg 是关系式
            eq_o = Eq(*sympify(arg.value))
            # 转化成 f(x) = ... 的格式
            eq = normalize_eq(eq_o)
            # f(x) = f(2a-x) 格式 提取出 a
            expr = eq.rhs
            a = is_faminusx(expr) / 2
            return a
        elif isinstance(arg, BaseFunc):
            # arg 是函数表达式，求函数对称中心
            a = HanShuDuiChengZhou().solver(arg).output[0].value[1]
            return a
        else:
            raise ValueError('arg must be of type BaseFunceq or BaseFunc')

    def solver(self, *args):
        sum_obj = args[-1]
        a = self.get_canshu(args[0])
        m = sum_obj.args[1][2]
        res = m * a

        self.steps.append(["", "由题意可推导出两个函数都关于 x = %s 轴对称" % a])
        self.steps.append(["", "所以 %s = %s" % (new_latex(sum_obj), res)])
        self.output.append(BaseNumber(res))
        return self


class JiaoDianDuiCheng(BaseFunction):
    """同一对称性求两函数的交点坐标关系"""

    def solver(self, *args):
        cls = [JiaoDianDuiCheng001, JiaoDianDuiCheng002]
        for item in cls:
            try:
                return item().solver(*args)
            except Exception:
                continue


if __name__ == '__main__':
    pass
